/*
脚本说明:获取扩展权限对应表的记录。
程序员:
*/

//脚本唯一关键字,通过该关键字找到对应脚本，每个脚本必须有该描述。
var v_script_id = 'authnodedyna';
//脚本状态,0:该脚本不许执行，1:该脚本可以执行
var v_status = '1';

//脚本逻辑写在run语句之间
if (run){
	var user = action.getSession().getAttribute("user");
	var authid = action.form.getString('authid');
	var sql = 'select * from h_auth_dyna where authid=' + authid;
	var map = action.dbUtil.findFirst("", sql);
	var tablename = map.get('tname');
	var vcol = map.get('vcol');
	var pcol = map.get('pcol');
	var condition = map.get('condition');
	if(user.getRolekind()>=3){
		var arr = condition.split('order'); //把order前后语句分开.
		var s = ' select distinct(pcolvalue) from v_auth_all where authid=' + authid ;
		if (user.getRoleids('roleid','or') == null){
			s += ' and userid = '+ user.getId() ;
		}else{
			s += ' and (' + user.getRoleids('roleid','or') + ' or userid = '+ user.getId() +')';
		}
		//println(s);
		if(arr.length==1){
			condition += ' and ' + pcol + ' in (' + s + ')';
		}else{
			condition = arr[0] + ' and ' + pcol + ' in (' + s + ') order ' +  arr[1];
		}
	}
	action.vars.put(action.V_TABLE_NAME,tablename);//指定获取数据的表名
	//必须使用别名 pcol, vcol
	action.vars.put(action.V_COLUMNS,pcol+' as pcol,'+vcol + ' as vcol'); 
	action.vars.put(action.V_CONDITION, condition );//数据检索条件
	action.vars.put(action.V_MAX_LINE,'5');//每页显示多少条
	action.vars.put(action.V_OUTPUT_FILE_FORMAT,'3');
	//println(tablename);
	//println( pcol+' as pcol,'+vcol + ' as vcol' );
	//println(condition);
	//println('select ' + pcol+' as pcol,'+vcol + ' as vcol' + ' from ' + tablename + condition);
}